package com.ruoyi.algorithm.controller;

import com.ruoyi.algorithm.entity.Manufacturer;
import com.ruoyi.algorithm.entity.TableStruct;
import com.ruoyi.algorithm.model.ListManufacturerRequestModel;
import com.ruoyi.algorithm.service.ManufacturerService;
import com.ruoyi.algorithm.service.TableStructService;
import com.beihu.sightplus.common.authorization.service.UserAuthorization;
import com.beihu.sightplus.common.response.ErrorMsgType;
import com.beihu.sightplus.common.response.PageAndConditionResult;
import com.beihu.sightplus.common.response.PageResult;
import com.beihu.sightplus.common.response.ResultData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


@RestController
@RequestMapping("/manufacturer")
@Api(value = "厂商管理",tags = {"厂商管理"})
public class ManufacturerController {

    @Autowired
    private ManufacturerService manufacturerService;
    @Resource
    TableStructService tableStructService;

    /**
     * 查询厂商下拉框数据
     */
    @GetMapping("getManufacturerDDBModelList")
    public ResultData getManufacturerDDBModelList() {
        return ResultData.success(manufacturerService.getManufacturerDDBModelList());
    }

    /**
     * 添加厂商
     *
     * @param manufacturer
     * @return
     */
    @PostMapping("addManufacturer")
    @ApiOperation("添加厂商接口")
    public ResultData addManufacturer(@RequestBody Manufacturer manufacturer) {
        return ResultData.success(manufacturerService.addManufacturer(manufacturer));
    }

    /**
     * 删除厂商接口
     *
     * @param id
     * @return
     */
    @DeleteMapping("deleteManufacturer")
    @ApiOperation("删除厂商接口")
    public ResultData deleteManufacturer(Long id) {
        boolean result = manufacturerService.deleteManufacturer(id);
        if (result) {
            return ResultData.success();
        } else {
            return ResultData.error(ErrorMsgType.Fail);
        }
    }


    /**
     * 修改厂商接口
     *
     * @param manufacturer
     * @return
     */
    @PutMapping("updateManufacturer")
    @ApiOperation("修改厂商接口")
    public ResultData updateManufacturer(@RequestBody Manufacturer manufacturer) {
        boolean result = manufacturerService.updateManufacturer(manufacturer);
        if (result) {
            return ResultData.success();
        } else {
            return ResultData.error(ErrorMsgType.Fail);
        }
    }

    /**
     * 获取厂商详情
     *
     * @param id
     * @return
     */
    @GetMapping("getOneManufacturer")
    @ApiOperation("获取厂商详情接口")
    public ResultData getOneManufacturer(Long id) {
        Manufacturer manufacturer = manufacturerService.getById(id);
        return ResultData.success(manufacturer);
    }

    /**
     * 分页查询厂商
     *
     * @param model
     * @return
     */
    @GetMapping("listManufacturer")
    @ApiOperation("分页查询厂商")
    public ResultData listManufacturer(@ApiParam ListManufacturerRequestModel model) {
        PageResult pageResult = manufacturerService.listManufacturer(model);
        List<TableStruct> tableField = tableStructService.getTableField(UserAuthorization.getUserAuthorization().getId(), "manufacturer");
        PageAndConditionResult<Object> result = new PageAndConditionResult<>(tableField, pageResult);
        return ResultData.success(result);
    }
}
